$(function(){
//	var $formUpload=$("#form_upload");
    /** ************增加文件*************** */
		var $formAdd=$("#form_add");
    	$("select[name=top]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=second]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=detail]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=content]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=project_mode]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$formAdd.find($("[name=cardinal]")).val(RandomNum(1,100)) ;
    	addCategory($formAdd);
    	addType();
    	addPlan();
    	addGrade();
    
	function checkAddForm(){
		if($("[name=name]").val() == ""){
			warnInput($("[name=name]"));
			return false;
		}
		if($("[name=type]").val() == 0){
			warnInput($("[name=type]"));
			return false;
		}
//		if($("[name=top]").val() == 0){
//			warnInput($("[name=top]"));
//			return false;
//		}
//		if($("[name=second]").val() == 0){
//			warnInput($("[name=second]"));
//			return false;
//		}
//		if($("[name=detail]").val() == 0){
//			warnInput($("[name=detail]"));
//			return false;
//		}
		if($("[name=cardinal]").val() == ""){
			warnInput($("[name=cardinal]"));
			return false;
		}
//		if($("[name=hierId]").val() == 0){
//			warnInput($("[name=hierId]"));
//			return false;
//		}
		return true;
	}
	
	 $("#btn_confirm_add").click(function() {
		 if($("#url").val() == null || $("#url").val() == ""){
			 alert("请先上传文件");
			 return false;
		 }
		if (checkAddForm()) {
			path = $("#url").val();
			name = $("[name=name]").val();
			typeId =$formAdd.find($("[name=type]")).val();
			tcId =$formAdd.find($("[name=top]")).val();
			scId =$formAdd.find($("[name=second]")).val();
//			mold = $formAdd.find("[name=mold]").val();
			size = $("#fileSize").val();
			hierId = $formAdd.find($("[name=hierId]")).val();
			grade = $formAdd.find($("[name=grade]")).val();
			cardinal = $formAdd.find($("[name=cardinal]")).val();
			
			dcIds = $formAdd.find($("[name=detail]")).val();
			
			if($formAdd.find($("[name=content]")).val() != 0){
				dcIds += ","+$formAdd.find($("[name=content]")).val() ;
			}
			
			if($formAdd.find($("[name=project_mode]")).val() != 0){
				dcIds += ","+ $formAdd.find($("[name=project_mode]")).val();
			}

			var price = 0;
			if($formAdd.find($("[name=price]")).val() != ""){
				price = $formAdd.find($("[name=price]")).val();
			}
			introduction = $("textarea[name='content1']").val();
			
			var tag = $("#tags").val();
			
			$.ajax({ // 此处用同步处理，避免出现val()比reload早的问题
				url : serverURL + "tag/addBatch?tags="+tag,
				type : "post",
				async : false,
				success : function(data) {
					if (data.code == 0) {
						$.post(serverURL + "file/add", {"authStatus":'1',"grade":grade,"hierId":hierId,"dcIds":dcIds,"name" : name,"path" : path,"size" : size,"tagIds" : data.data,"typeId" : typeId,"tcId" : tcId,"scId" : scId,"introduction":introduction,"price":price,"cardinal":cardinal}, function(data) {
							if (data.code == 0) {
									alert("添加成功");
									  cook.addCookie("typeId", typeId);//文件类型
									  cook.addCookie("tcId", tcId);//一级分类
									  cook.addCookie("scId", scId);//二级分类
									  cook.addCookie("detail", $formAdd.find($("[name=detail]")).val());//详情分类1
									  cook.addCookie("content", $formAdd.find($("[name=content]")).val());//详情分类2
									  cook.addCookie("project_mode", $formAdd.find($("[name=project_mode]")).val());//详情分类3
									  
									  cook.addCookie("hierId", hierId);//规划体系
									  cook.addCookie("grade", grade);//等级
									  
									window.location.href="file.htm";
								} else if (data.code != -1) {
									alert("添加失败,请稍后重试");
								}
						});
	
					} else if (data.code != -1) {
						alert("数据添加失败");
					}
				}
			});		
			
		}
	})
	 /**添加规划体系* */
function addPlan(){
	$.get("view/plan/getList",{"all":true}, function(data) {
		$("#form_add").find("[name=hierId]").empty().html("<option value='0' selected>请选择规划体系...</option>");
		$.each(data.data.list,function(i,k){
		     if( k.hierId == cook.getCookie("hierId")){
					$("#form_add").find("[name=hierId]").append("<option value='" + k.hierId + "' selected>" + k.hierName+ "</option>");
		     }else{
					$("#form_add").find("[name=hierId]").append("<option value='" + k.hierId + "'>" + k.hierName+ "</option>");
		     }
		});
	}, "json");
} 
	/**添加等级***/
function addGrade(){
	if(cook.getCookie("grade") == 1){
		$("#form_add").find("[name=grade]").empty().html("<option value='0' >请选择等级...</option><option value='1'  selected>入门</option><option value='2' >进阶</option>");
	}else if(cook.getCookie("grade") == 2){
		$("#form_add").find("[name=grade]").empty().html("<option value='0' >请选择等级...</option><option value='1' >入门</option><option value='2' selected>进阶</option>");
	}else{
		$("#form_add").find("[name=grade]").empty().html("<option value='0' selected>请选择等级...</option><option value='1' >入门</option><option value='2' >进阶</option>");
	}
 }
	/***********添加文件类型****************/
	function addType(){
		$.get("type/list", function(data) {
			$formAdd.find("[name=type]").empty();
			$.each(data.data, function(i, obj) {
				if(obj.typeId==cook.getCookie("typeId")){
					$formAdd.find("[name=type]").append("<option value='" + obj.typeId + "' selected>" + obj.typeName+ "</option>");
				}else{
					$formAdd.find("[name=type]").append("<option value='" + obj.typeId + "'>" + obj.typeName+ "</option>");
				}
			});
		}, "json");
	}
	 //标签
//	 function onAddTag(tag) {
//	        alert("Added a tag: " + tag);
//	    }
//	    function onRemoveTag(tag) {
//	        alert("Removed a tag: " + tag);
//	    }
//
//	    function onChangeTag(input, tag) {
//	        alert("Changed a tag: " + tag);
//	    }
	 
        $("#tags").tagsInput({
            width: 'auto',
            onChange: function (elem, elem_tags) {
//                var languages = ['php', 'ruby', 'javascript'];
//                $('.tag', elem_tags).each(function () {
//                    if ($(this).text().search(new RegExp('\\b(' + languages.join('|') + ')\\b')) >= 0)
//                        $(this).css('background-color', 'yellow');
//                });
            },
//            onAddTag:function(elem, elem_tags){
//            	alert(elem+$("#tags_1").val());
//            },
//           onRemoveTag:function(elem, elem_tags){
//            	alert(elem+$("#tags_1").val());
//            }
        });
        
    	/****生成随机数min ≤ r ≤ max***/
    	function RandomNum(Min,Max){
    	      var Range = Max - Min;
    	      var Rand = Math.random();
    	      var num = Min + Math.round(Rand * Range); //四舍五入
    	      return num;
    	}
});